﻿namespace GameStudio.Framework.GraphicsEngine
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;

    using SlimDX;
    using SlimDX.Direct3D9;
    using System.IO;

    public class Mesh<T> : VisualObjectBase
        where T : Vertex
    {
        #region Fields

        private SlimDX.Direct3D9.Material[] _materials;
        protected SlimDX.Direct3D9.Mesh _mesh;
        private SlimDX.Direct3D9.Texture[] _textures;

        #endregion Fields

        #region Properties


        public MeshFlags OpenMeshFlags { private set; get; }

        public T[] VertexBuffer
        {
            set;
            get;
        }

        #endregion Properties

        #region Methods

        public override void Initilize()
        {
            //_mesh = new Mesh(DeviceContext.Current.Device, 1, 7, OpenMeshFlags, Vertex.Format);
            //var dataStream = _mesh.LockVertexBuffer(LockFlags.Discard);
            //dataStream.Write();
            //_mesh.UnlockVertexBuffer();

            //_materials = new Material[];

            //_textures = new Texture[];
        }

        public void LoadXfile(string fileName)
        {
            _mesh = Mesh.FromFile(DeviceContext.Current.Device, fileName, OpenMeshFlags);
        }


        public void AddMaterials(string[] colors)
        {
            _materials = new Material[colors.Length];
            ColorConverter Conv = new ColorConverter();
            for (int i = 0; i < colors.Length; i++)
            {
                _materials[i].Diffuse = (Color)Conv.ConvertFromString(colors[i]);
                _materials[i].Ambient = (Color)Conv.ConvertFromString(colors[i]);
            }
           
        }


        public override void Render()
        {
            //_mesh = new Mesh(DeviceContext.Current.Device, 1, 7, OpenMeshFlags, Vertex.Format);
            base.Render();
            //for (int i = 0; i < _materials.Length; i++)
            //{
            //    //if (_textures[i] != null)
            //    //{
            //    //    DeviceContext.Current.Device.SetTexture(0, _textures[i]);
            //    //}

            //   DeviceContext.Current.Device.Material = _materials[i];
            //    _mesh.DrawSubset(0);
            //}
            ColorConverter Conv = new ColorConverter();
            DeviceContext.Current.Device.Material = new Material()
            {
                Diffuse = _materials[0].Diffuse,
                Ambient = _materials[0].Ambient//(Color)Conv.ConvertFromString("#FF2E8B57")
            };
            DeviceContext.Current.Device.SetRenderState(RenderState.FillMode, FillMode.Solid);

           // VertexBuffer[0].Color = ((Color)Conv.ConvertFromString("#FF2E8B57")).ToArgb();
            //_mesh.DrawSubset(0);
            //this.Matrix = SlimDX.Matrix.Identity;
        }


        #endregion Methods
    }
}